<?php

/**
 * This is the model class for table "{{projects}}".
 *
 * The followings are the available columns in table '{{projects}}':
 * @property integer $id
 * @property string $create_at
 * @property string $modify_at
 * @property string $create_user_id
 * @property string $update_user_id
 * @property integer $county_id
 * @property string $name
 * @property string $description
 * @property string $type
 * @property double $longitude
 * @property double $latitude
 * @property double $pre_invest_central
 * @property double $pre_invest_province
 * @property double $pre_invest_prefecture
 * @property double $pre_invest_county
 * @property double $invest_central
 * @property double $invest_province
 * @property double $invest_prefecture
 * @property double $invest_county
 * @property string $scale
 * @property string $feasibility_research
 * @property string $init_design
 * @property string $financial_assess
 * @property string $bidding
 * @property string $owner
 * @property string $init_design_depart
 * @property string $supervision_depart
 * @property string $construction_depart
 * @property string $quality_depart
 * @property integer $pre_work_status
 * @property integer $advanced_info_status
 * @property integer $qualification_status
 * @property integer $divide_projects_status
 * @property integer $is_snapshot
 * @property integer $is_live
 *
 * The followings are the available model relations:
 * @property Cameras[] $camerases
 * @property Counties $county
 * @property Unitprojects[] $unitprojects
 */
class Project extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Project the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{projects}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('county_id, name, type', 'required'),
			array('county_id, pre_work_status, advanced_info_status, qualification_status, 
					divide_projects_status', 'numerical', 'integerOnly'=>true),
			array('longitude, latitude, pre_invest_central, pre_invest_province, 
					pre_invest_prefecture, pre_invest_county, invest_central, invest_province, 
					invest_prefecture, invest_county, is_snapshot, is_live', 'numerical'),
			array('name', 'length', 'max'=>48),
			array('description, scale, feasibility_research, init_design, financial_assess, bidding, 
					owner, init_design_depart, supervision_depart, 
					construction_depart, quality_depart, type', 'length', 'max'=>255),
			array('create_user_id, update_user_id', 'length', 'max'=>10),
			array('modify_at', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, county_id, name, description, type, longitude, latitude, pre_invest_central, pre_invest_province, 
					pre_invest_prefecture, pre_invest_county, invest_central, invest_province, invest_prefecture, 
					invest_county, scale, feasibility_research, init_design, financial_assess, 
					bidding, owner, init_design_depart, supervision_depart, construction_depart, 
					quality_depart, pre_work_status, advanced_info_status, qualification_status, 
					divide_projects_status, create_at, modify_at, create_user_id, update_user_id, is_snapshot, is_live', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'camerases' => array(self::HAS_MANY, 'Camera', 'project_id'),
			'unitProjects' => array(self::HAS_MANY, 'UnitProject', 'project_id'),
			'county' => array(self::BELONGS_TO, 'County', 'county_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'create_at' => Yii::t('common','Create At'),
			'modify_at' => Yii::t('common','Modify At'),
			'create_user_id' => 'Create User',
			'update_user_id' => 'Update User',
			'county_id' => Yii::t('project','Belonging County'),
			'name' => Yii::t('project','Name'),
			'description' => Yii::t('project','Description'),
			'type' => Yii::t('project','Type'),
			'longitude' => Yii::t('project','Longitude'),
			'latitude' => Yii::t('project','Latitude'),
			'pre_invest_central' => Yii::t('project','Pre Invest Central'),
			'pre_invest_province' => Yii::t('project','Pre Invest Province'),
			'pre_invest_prefecture' => Yii::t('project','Pre Invest Prefecture'),
			'pre_invest_county' => Yii::t('project','Pre Invest County'),
			'invest_central' => Yii::t('project','Invest Central'),
			'invest_province' => Yii::t('project','Invest Province'),
			'invest_prefecture' => Yii::t('project','Invest Prefecture'),
			'invest_county' => Yii::t('project','Invest County'),
			'scale' => Yii::t('project','Scale'),
			'feasibility_research' => Yii::t('project','Feasibility Research'),
			'init_design' => Yii::t('project','Init Design'),
			'financial_assess' => Yii::t('project','Financial Assess'),
			'bidding' => Yii::t('project','Bidding'),
			'owner' => Yii::t('project','Owner'),
			'init_design_depart' => Yii::t('project','Init Design Depart'),
			'supervision_depart' => Yii::t('project','Supervision Depart'),
			'construction_depart' => Yii::t('project','Construction Depart'),
			'quality_depart' => Yii::t('project','Quality Depart'),
			'pre_work_status' => Yii::t('project','Pre Work Status'),
			'advanced_info_status' => Yii::t('project','Advanced Info Status'),
			'qualification_status' => Yii::t('project','Qualification Status'),
			'divide_projects_status' => Yii::t('project','Divide Projects Status'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('create_at',$this->create_at,true);
		$criteria->compare('modify_at',$this->modify_at,true);
		$criteria->compare('create_user_id',$this->create_user_id,true);
		$criteria->compare('update_user_id',$this->update_user_id,true);
		$criteria->compare('county_id',$this->county_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('type',$this->description,true);		
		$criteria->compare('longitude',$this->longitude);
		$criteria->compare('latitude',$this->latitude);
		$criteria->compare('pre_invest_central',$this->pre_invest_central);
		$criteria->compare('pre_invest_province',$this->pre_invest_province);
		$criteria->compare('pre_invest_prefecture',$this->pre_invest_prefecture);
		$criteria->compare('pre_invest_county',$this->pre_invest_county);
		$criteria->compare('invest_central',$this->invest_central);
		$criteria->compare('invest_province',$this->invest_province);
		$criteria->compare('invest_prefecture',$this->invest_prefecture);
		$criteria->compare('invest_county',$this->invest_county);
		$criteria->compare('scale',$this->scale,true);
		$criteria->compare('feasibility_research',$this->feasibility_research,true);
		$criteria->compare('init_design',$this->init_design,true);
		$criteria->compare('financial_assess',$this->financial_assess,true);
		$criteria->compare('bidding',$this->bidding,true);
		$criteria->compare('owner',$this->owner,true);
		$criteria->compare('init_design_depart',$this->init_design_depart,true);
		$criteria->compare('supervision_depart',$this->supervision_depart,true);
		$criteria->compare('construction_depart',$this->construction_depart,true);
		$criteria->compare('quality_depart',$this->quality_depart,true);
		$criteria->compare('pre_work_status',$this->pre_work_status);
		$criteria->compare('advanced_info_status',$this->advanced_info_status);
		$criteria->compare('qualification_status',$this->qualification_status);
		$criteria->compare('divide_projects_status',$this->divide_projects_status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	protected function beforeSave()
	{
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->create_at = date('Y-m-d H:i:s');
				$this->modify_at = date('Y-m-d H:i:s');
				$this->create_user_id = Yii::app()->user->id;
				$this->update_user_id = Yii::app()->user->id;
			}else{
				$this->modify_at = date('Y-m-d H:i:s');
				$this->update_user_id = Yii::app()->user->id;
			}
			return true;
		}else{
			return false;
		}
	}
}